package com.kycxika.app.activity.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.kycxika.app.R;
import com.kycxika.app.activity.LoginActivity;
import com.kycxika.app.activity.MainActivity;
import com.kycxika.app.activity.pic.SelfInfoActivity;
import com.kycxika.app.dbutils.IntImp.KycXkIApi;
import com.kycxika.app.utils.SharedPreferencesHelp;
import com.kycxika.app.view.CircleImageView;
import com.zhy.android.percent.support.PercentRelativeLayout;


/**
 * Created by DELL on 2017/5/2.
 */
public class MySelfFragment extends Fragment{

    private View mView;
    private CircleImageView mFamily_image;
    private TextView family_self;
    private TextView family_name;
    private PercentRelativeLayout out_login;
    private Boolean userRegistionStatu;
    private KycXkIApi api;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = View.inflate(getActivity(), R.layout.activity_family_school, null);
        initView();
        initData();
        initListener();
        return mView;
    }

    private void initView() {
        mFamily_image = (CircleImageView) mView.findViewById(R.id.family_image);
        family_self = (TextView) mView.findViewById(R.id.family_self);   //个人信息
        family_name = (TextView) mView.findViewById(R.id.family_name);
        out_login = (PercentRelativeLayout) mView.findViewById(R.id.out_login);

        api = KycXkIApi.getInstance(getContext());
        userRegistionStatu = SharedPreferencesHelp.getInstance().getLoginStatus();//api.getUserRegistionStatu();  //判断是否登录

        if(userRegistionStatu) {
            out_login.setVisibility(View.VISIBLE);
            out_login.setEnabled(true);
            family_name.setText(SharedPreferencesHelp.getInstance().getUserNick());
            if(!TextUtils.isEmpty(SharedPreferencesHelp.getInstance().getUserHeader())) {
                Log.i("TAG","header ------ " +SharedPreferencesHelp.getInstance().getUserHeader());
                /*Bitmap bitmap = BitmapFactory.decodeFile(SharedPreferencesHelp.getInstance().getUserHeader());
                mFamily_image.setImageBitmap(bitmap);*/
                Glide.with(this)
                        .load(SharedPreferencesHelp.getInstance().getUserHeader())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸
                        .thumbnail(0.1f)//先显示缩略图  缩略图为原图的1/10
                        .error(R.drawable.ce_01)
                        .into(new GlideDrawableImageViewTarget(mFamily_image) {
                            @Override
                            public void onLoadStarted(Drawable placeholder) {
                                super.onLoadStarted(placeholder);
                            }

                            @Override
                            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                                super.onLoadFailed(e, errorDrawable);
                            }

                            @Override
                            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                                super.onResourceReady(resource, animation);
                            }
                        });
            }else {
                mFamily_image.setImageResource(R.drawable.ce_01);
            }
        }else {
            out_login.setVisibility(View.INVISIBLE);
            out_login.setEnabled(false);
            family_name.setText("请登录");
            mFamily_image.setImageResource(R.drawable.ce_01);
        }
    }

    private void initData() {

    }

    private void initListener() {
        mFamily_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!userRegistionStatu) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }else {

                }
            }
        });

        mView.findViewById(R.id.system_notif).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统通知
                if(userRegistionStatu) {

                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        mView.findViewById(R.id.system_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //系统设置
                if(userRegistionStatu) {

                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        mView.findViewById(R.id.out_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //退出登录
                SharedPreferencesHelp.getInstance().saveLoginStatus(false);
                startActivity(new Intent(getActivity(), LoginActivity.class));
                MainActivity.instance.finish();
            }
        });

        family_self.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //个人设置
                if(userRegistionStatu) {
                    startActivity(new Intent(getActivity(), SelfInfoActivity.class));
                }else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                //MainActivity.instance.finish();
            }
        });
    }
}
